package com.qzsoft.zhjd.materiel.modules.materieldataabnormal.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.platform.security.user.SecurityUser;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.enums.YesOrNo;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataDisableDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.MaterielDataSourceEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.entity.MaterielDataEntity;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;
import com.qzsoft.zhjd.materiel.modules.materieldataabnormal.service.MaterielDataAbnormalService;
import com.qzsoft.zhjd.materiel.modules.materieldevice.entity.MaterielDeviceEntity;
import com.qzsoft.zhjd.materiel.modules.materieldevice.service.MaterielDeviceService;

/**
 * @ClassName MaterielDataAbnormalServiceImpl
 * @Description 异常磅单处理接口
 * @author linhuawei
 * @since 2022-04-08
 */
@Service
public class MaterielDataAbnormalServiceImpl implements MaterielDataAbnormalService {

    @Autowired
    private MaterielDataService materielDataService;

    @Autowired
    private MaterielDeviceService materielDeviceService;

    @Override
    public void updateById(MaterielDataEntity entity) {
        // 判断磅单是否存在
        MaterielDataEntity temp = materielDataService.getById(entity.getId());
        if(null == temp) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "磅单数据不存在");
        }
        
        // 判断是否废弃磅单，废弃磅单不可修改
        if(YesOrNo.yes.getValue() == entity.getDisable()) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "当前磅单已废弃，不可修改");
        }
        
        // 参数校验
        check(entity);
        
        
        // 毛重、皮重不可修改
        entity.setWeightCode(temp.getWeightCode());
        entity.setGrossWeight(temp.getGrossWeight());
        entity.setTareWeight(temp.getTareWeight());
        entity.setEntryTime(temp.getEntryTime());
        entity.setExitTime(temp.getExitTime());
        entity.setOperatorId(temp.getOperatorId());
        entity.setEntryImageOne(temp.getEntryImageOne());
        entity.setEntryImageTwo(temp.getEntryImageTwo());
        entity.setEntryImageThree(temp.getEntryImageThree());
        entity.setEntryImageFour(temp.getEntryImageFour());
        entity.setExitImageOne(temp.getExitImageOne());
        entity.setExitImageTwo(temp.getExitImageTwo());
        entity.setExitImageThree(temp.getExitImageThree());
        entity.setExitImageFour(temp.getExitImageFour());
        entity.setWaybillImage(temp.getWaybillImage());
        entity.setPrintTimes(temp.getPrintTimes());
        entity.setChanged(YesOrNo.yes.getValue());// 数据变化，需要跟磅房做同步
        
        // 修改磅单
        materielDataService.updateById(entity);
    }
    
    @Override
    public void disable(MaterielDataDisableDto dto) {
        // 判断磅单是否存在
        MaterielDataEntity temp = materielDataService.getById(dto.getId());
        if(null == temp) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "磅单数据不存在");
        }
        
        // 更新磅单状态
        UpdateWrapper<MaterielDataEntity> updateWrapper = new UpdateWrapper<MaterielDataEntity>();
        UserDetail user = SecurityUser.getUser();
        updateWrapper.eq("id", dto.getId());
        updateWrapper.set("disable", YesOrNo.yes.getValue());
        updateWrapper.set("disable_reason", dto.getDisableReason());
        updateWrapper.set("updater", user.getId());
        updateWrapper.set("update_date", new Date());
        updateWrapper.set("changed", YesOrNo.yes.getValue());// 数据变化，需要跟磅房做同步
        materielDataService.update(updateWrapper);
    }

    @Override
    public String getLastYear(Map<String, Object> params) {
        params.put("weightDataSource", MaterielDataSourceEnum.INPUT.getValue());
        return materielDataService.getLastYear(params);
    }

    private void check(MaterielDataEntity entity) {
        // 判断收发料类型，根据收发料类型判断供应商、用料项目/单位是否为空
        if (WeightTypeEnum.RECEIVE.getValue().equals(entity.getWeightType())) {
            if (ObjectUtils.isEmpty(entity.getSupplierId())) {
                RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "供应商不可为空");
            }
            entity.setProjectId(null);// 收料不需要用料项目/单位
        } else if (WeightTypeEnum.SEND.getValue().equals(entity.getWeightType())) {
            if (ObjectUtils.isEmpty(entity.getProjectId())) {
                RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "用料项目/单位不可为空");
            }
            entity.setSupplierId(null);// 发料不需要供应商
        } else {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "未知的收发料类型");
        }

        // 判断地磅是否存在
        MaterielDeviceEntity device = materielDeviceService.getByCode(entity.getDeviceCode());
        if (null == device) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "地磅设备不存在");
        }

        // 设置基本信息
        entity.setBaseCode(device.getBaseCode());// 基地信息
        entity.setDeviceCode(device.getCode());// 设备编号
    }

}
